const express = require('express');
const multer = require('multer');
const path = require('path');
const router = express();

// 配置存储路径和文件名
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/img/course/'); // 图片保存路径
    },
    filename: (req, file, cb) => {
        // console.log(file);
        // const ext = path.extname(file.originalname); // 提取文件扩展名
        const fileName = file.originalname; // 文件名使用时间戳
        cb(null, fileName);
    },
});

// 配置 multer 中间件
const upload = multer({ storage });

router.post('/upload/courseImage', upload.single('courseImg'), (req, res) => {
    if (!req.file) {
        return res.status(400).send({ message: '未检测到文件' });
    }
    const filePath = `/img/course/${req.file.filename}`; // 返回相对路径
    res.send({ filePath, message: '图片上传成功' });
});

module.exports = router;